package com.dream.dao.impl;


import com.dream.dao.StudentDao;
import com.dream.pojo.Student;
import com.dream.utils.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {

    @Override
    public List<Student> findStudentsByClazzId(String clazzId) {
        return DBUtil.commonQuery(Student.class, "select * from student where clazz_id=?", clazzId);
    }

    @Override
    public long totalCount(String stuName) {
        String sql = "select count(student.stu_id) FROM student where 1=1";
        StringBuilder sb = new StringBuilder(sql);
        if (stuName != null && !"".equals(stuName)) {
            sb.append(" and student.stu_name like '%" + stuName + "%'");
        }
        return DBUtil.queryCount(sb.toString());
    }

    @Override
    public List<Student> list(String stuName, long pageIndex, long pageSize) {
        String sql = "select * FROM student where 1=1";
        StringBuilder sb = new StringBuilder(sql);
        if (stuName != null && !"".equals(stuName)) {
            sb.append(" and student.stu_name like '%" + stuName + "%'");
        }
        sb.append(" limit ?,?");
        return DBUtil.commonQuery(Student.class, sb.toString(), (pageIndex - 1) * pageSize, pageSize);
    }

    @Override
    public int add(Student student) {
        try {
            DBUtil.commonUpdate("insert into student values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                    student.getStuId(),
                    student.getStuName(),
                    student.getSex(),
                    student.getBirthplace(),
                    student.getBirthday(),
                    student.getPhoneNumber(),
                    student.getKinsfolkName(),
                    student.getKinsfolkPhoneNumber(),
                    student.getDormitoryId(),
                    student.getClazzId(),
                    student.getGrade(),
                    student.getRemark(),
                    student.getGmtCreate(),
                    student.getGmtModify()
            );
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Student findById(String stuId) {
        ArrayList<Student> list = DBUtil.commonQuery(Student.class, "select * from student where stu_id=?", stuId);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public int update(Student student) {
        try {
            DBUtil.commonUpdate("update student set stu_name=?,sex=?,birthplace=?,birthday=?," +
                            "phone_number=?,kinsfolk_name=?,kinsfolk_phone_number=?,dormitory_id=?," +
                            "clazz_id=?,grade=?,remark=?,gmt_modify=? where stu_id=?",
                    student.getStuName(),
                    student.getSex(),
                    student.getBirthplace(),
                    student.getBirthday(),
                    student.getPhoneNumber(),
                    student.getKinsfolkName(),
                    student.getKinsfolkPhoneNumber(),
                    student.getDormitoryId(),
                    student.getClazzId(),
                    student.getGrade(),
                    student.getRemark(),
                    student.getGmtModify(),
                    student.getStuId()
            );
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteById(String stuId) {
        try {
            DBUtil.commonUpdate("delete from  student where stu_id=?", stuId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Student> findStudentsByDormitoryId(String dormitoryId) {
        return DBUtil.commonQuery(Student.class, "select * from student where dormitory_id=?", dormitoryId);
    }
}
